﻿using System.Drawing;
using GraphDrawing2.V2.Concrete.Model;
using GraphDrawing2.V2.Graph.Model;

namespace GraphDrawing2.V2.Concrete.States
{
    public class LinkingState : IState
    {
        readonly Model2 model;
        readonly ConnectorOrigin origin;
        readonly PointF startPoint;
        readonly Link link;

        public LinkingState(Model2 model, ConnectorHitTest source)
        {
            this.model = model;
            origin = source.Origin;
            var position = origin.Owner.Position.Sum(origin.Connector.Position);
            var size = origin.Connector.Bounds.Size;
            startPoint = new PointF(position.X + size.Width/2f, position.Y + size.Height/2f);
            link = new Link(startPoint);
            model.ElementsModel.Add(link);
        }

        public void ProcessMouseMove()
        {
            var point = model.ViewPort.ToWorld(model.CanvasModel.MousePosition);
            link.UpdateEndPoint(point);
            model.View.UpdateView();
        }

        public void ProcessMouseUp()
        {
            model.ElementsModel.Remove(link);
        }

        public void ProcessKeyPressed()
        {
        }
    }
}